//
//  MCReceivedHBDetailViewController.m
//  XDYL
//
//  Created by MC on 2018/3/20.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCReceivedHBDetailViewController.h"
#import "MCReceivedHBDetailTableViewCell.h"
#import "MCReceivedHBDetailHeaderView.h"
#import <MJRefresh/MJRefresh.h>
#import "MCHongBaoDetailModel.h"

@interface MCReceivedHBDetailViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic, strong) UITableView * tableView;
@property(nonatomic, strong) NSMutableArray <MCDetailListModel *>* dataMarray;
@property(nonatomic, strong) MCReceivedHBDetailHeaderView * headerView;
@property(nonatomic, strong) MCHongBaoDetailModel * hongBaoDetailModel;
@property(nonatomic, strong) ExceptionView * exceptionView;



@end

@implementation MCReceivedHBDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
    [self refreashData];
    
    
}


#pragma mark==================setProperty======================
-(void)setProperty{
    self.title=@"红包详情";
    self.view.backgroundColor=RGB(231,231,231);
    [self setNavigationState:NO];
    self.navigationController.navigationBar.translucent = NO;

    _dataMarray=[[NSMutableArray alloc]init];

}
- (void)setNavigationState:(BOOL)state{
    if (state) {
        self.edgesForExtendedLayout=UIRectEdgeBottom;
    }
    else{
        self.edgesForExtendedLayout=UIRectEdgeTop;
    }
    self.navigationController.navigationBar.translucent = state;
    [self.navigationController setNavigationBarHidden:state animated:YES];
}
-(MCReceivedHBDetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView=[[MCReceivedHBDetailHeaderView alloc]init];
        //        _headerView.delegate=self;
        _headerView.frame=CGRectMake(0, 0, G_SCREENWIDTH, [MCReceivedHBDetailHeaderView computeHeight:nil]);
    }
    return _headerView;
}

#pragma mark==================createUI======================
-(void)createUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
//    _tableView.layer.cornerRadius=5;
    _tableView.clipsToBounds=YES;
    _tableView.tableHeaderView=self.headerView;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreashData];
    }];
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self.view addSubview:_tableView];
    CGFloat H =0;
    if (self.navigationController.navigationBar.translucent) {
        H = 64;
    }
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(H);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    
}

#pragma mark-加载数据
- (void)refreashData{
    __weak __typeof__ (self) wself = self;
//    Sysid
//    红包sysid
//    Flag
//    详情0：收到 1发出

    NSDictionary * dic =@{
                          @"Sysid":_Sysid,
                          @"IsHistory":[NSNumber numberWithBool:self.IsHistory1],
                          @"Flag":@"0"//    详情0：收到 1发出
                          };
    
    MCHongBaoDetailModel * hongBaoDetailModel = [[MCHongBaoDetailModel alloc]initWithDic:dic];
    [hongBaoDetailModel refreashDataAndShow];
    self.hongBaoDetailModel = hongBaoDetailModel;
    
    hongBaoDetailModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        
        [wself.tableView.mj_footer endRefreshing];
        [wself.tableView.mj_header endRefreshing];
        
        wself.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeRequestFailed];
//        wself.exceptionView.originY=64;
        wself.exceptionView.actionColor=RGB(255,69,69);
        ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeRequestFailed handler:^(ExceptionViewAction *action) {
            [wself.exceptionView dismiss];
            wself.exceptionView = nil;
            [wself refreashData];
        }];
        [wself.exceptionView addAction:action];
        [wself.exceptionView showInView:wself.view];
        
    };
    
    hongBaoDetailModel.callBackSuccessBlock = ^(id manager) {
        
        [wself.tableView.mj_footer endRefreshing];
        [wself.tableView.mj_header endRefreshing];
        
        [wself setData:manager];
        
    };
    
}

-(void)setData:(NSDictionary *)dic{
    
    [_dataMarray removeAllObjects];
    MCHongBaoDetailModel * model = [MCHongBaoDetailModel mj_objectWithKeyValues:dic];
    [_dataMarray addObjectsFromArray:model.DetailList];
    
    _headerView.dataSource=model.DetailTotal;
    
    [self.tableView reloadData];

}


-(void)loadMoreData{
    
}

#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataMarray.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return [MCReceivedHongBaoHeaderView computeHeight:nil];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    return nil;
//}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MCReceivedHBDetailTableViewCell computeHeight:nil];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier =[NSString stringWithFormat:@"MCReceivedHBDetailTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    MCReceivedHBDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[MCReceivedHBDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataMarray.count>0) {
        MCDetailListModel * model = _dataMarray[indexPath.row];
        cell.dataSource=model;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

















